﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Things.ThingsWeb;

namespace Things.Web.Components
{
    public partial class MainComments : System.Web.UI.UserControl
    {
        Things.BLL.GoodsComments gcBLL = new BLL.GoodsComments();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        public void BindData()
        {

            DataSet ds = new DataSet();
            ds = gcBLL.GetMainComments();
            DataRowCollection rows = ds.Tables[0].Rows;

            Random random = new Random();
            while (rows.Count > 5)
            {
                int index = random.Next(0, rows.Count);
                rows.RemoveAt(index);
            }

            gridView.DataSource = ds;
            gridView.DataBind();
        }

        protected string GetGoodsName(object obj, int length)
        {
            string temp = obj != null ? obj.ToString() : string.Empty;
            string goodsName = PageUtility.GetLimitedString(temp, length);
            if (string.IsNullOrEmpty(goodsName) == true)
            {
                goodsName = temp;
            }
            return goodsName;
        }

        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Image image2 = (Image)e.Row.FindControl("Image2");
                HiddenField hfRate = (HiddenField)e.Row.FindControl("hfRate");
                int rate = 0;

                if (hfRate != null)
                {
                    if (int.TryParse(hfRate.Value, out rate) == false)
                    {
                        rate = 0;
                    }
                }
                if (image2 != null)
                {
                    if (rate > 0 && rate <= 5)
                    {
                        image2.ImageUrl = "~/style/images/" + rate.ToString() + "xing.jpg";
                    }
                    
                }
            }
        }
    }
}